﻿using Helper;
using Helper.Models;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Starehe.ViewModels
{
    public class NewGalleryItemVM: ViewModelBase
    {
        bool isSelecting;
        private Uri mediaSource;
        public NewGalleryItemVM()
            : base()
        {
            InitVars();
            CreateCommands();
        }


        protected override void CreateCommands()
        {
            BrowseCommand = new RelayCommand(o =>
            { MediaSource = FileHelper.BrowseImageAsUri();
             IsSelecting = false; 
            }, o => true);

            SaveCommand = new RelayCommand(async o => 
            {
                bool succ = await DataAccess.SaveNewGalleryItemAsync(mediaSource);
                if (succ)
                    Reset();
            }, o => !isSelecting && mediaSource!=null);
        }

        protected override void InitVars()
        {
            
            Title = "ADD NEW ITEM";
            IsSelecting = true;
            mediaSource = new Uri("pack://application:,,,/Resources/noImage.png");
        }

        public override void Reset()
        {
            IsSelecting = true;
        }

        public Uri MediaSource
        {
            get { return mediaSource; }
            set
            {
                if (mediaSource!=value)
                {
                    mediaSource = value;
                    NotifyPropertyChanged("MediaSource");
                }
            }
        }

        public bool IsSelecting
        {
            get { return isSelecting; }
            set
            {
                if (isSelecting != value)
                {
                    isSelecting = value;
                    NotifyPropertyChanged("IsSelecting");
                }
            }
        }

      

        public ICommand BrowseCommand
        {
            get;
            private set;
        }

        public ICommand SaveCommand
        {
            get;
            private set;
        }
    }
}
